// TypeScript file
module starlingswf
{
export class SwfMovieClip extends SwfSprite implements ISwfAnimation
{
    private ownerSwf: Swf;
    private frames: any[];
    private labels: any[];
    private displayObjects: Object;

    private startFrame: number;
    private endFrame: number;
    private curFrame: number;
    private curLabel: string;

    private frameInfos: any[];

    private isPlay: boolean = false;
    public isLoop: boolean = true;

    private completeFunction: Function = null;      //播放完毕回调函数
    private hasCompleteListener: boolean = false;   //是否监听播放完毕事件

    constructor(frames: any[], labels: any[], displayObjects: Object, ownerSwf: Swf)
    {
        super();

        this.frames = frames;
        this.labels = labels;
        this.displayObjects = displayObjects;
        this.ownerSwf = ownerSwf;

        this.startFrame = 0;
        this.endFrame = this.frames.length - 1;
        this.setCurFrame(this.startFrame);
        this.play();
    }

    private setCurFrame(curFrame: number): void
    {
        this.removeChildren();
        this.curFrame = curFrame;
        this.frameInfos = this.frames[this.curFrame];

        let data: any[];
        let display: egret.DisplayObject;
        let textfield: egret.TextField;
        let useIdx: number;
        let length: number = this.frameInfos.length;

        for (let i: number = 0; i < length; ++i)
        {
            data = this.frameInfos[i];
            useIdx = data[10];
            display = this.displayObjects[data[0]][useIdx];
            display.skewX = data[6];
            display.skewY = data[7];
            display.alpha = data[8];
            display.name = data[9];

            display.x = data[2];
            display.y = data[3];

            if (data[1] == Swf.dataKey_Scale9)
            {
                display.width = data[11];
                display.height = data[12];
            }
            else
            {
                display.scaleX = data[4];
                display.scaleY = data[5];
            }

            this.addChild(display);

            if (data[1] == Swf.dataKey_TextField)
            {
                textfield = <egret.TextField>display;
                textfield.width = data[11];
                textfield.height = data[12];

                textfield.textColor = data[14];
                textfield.size = data[15];
                textfield.textAlign = data[16];

                if (data[19] && data[19] != "\r" && data[19] != "")
                {
                    textfield.text = data[19];
                }

            }
        }

    }

    public getCurFrame(): number
    {
        return this.curFrame;
    }
    public play(): void
    {
        this.isPlay = true;
        this.ownerSwf.swfUpdateManager.addSwfAnimation(this);
        let k: string;
        let arr: any[];
        let l: number;

        for (k in this.displayObjects)
        {
            if (0 == k.indexOf(Swf.dataKey_MovieClip))
            {
                arr = this.displayObjects[k];
                l = arr.length;
                for (let i: number = 0; i < 1; ++i)
                {
                    (<SwfMovieClip>arr[i]).play();
                }
            }
        }
    }

    public stop(stopChild: boolean = true): void
    {
        this.isPlay = false;
        this.ownerSwf.swfUpdateManager.removeSwfAnimation(this);

        if (!stopChild) return;

        let k: string;
        let arr: any[];
        let l: number;
        for (k in this.displayObjects)
        {
            if (0 == k.indexOf(Swf.dataKey_MovieClip))
            {
                arr = this.displayObjects[k];
                l = arr.length;
                for (let i: number = 0; i < 1; ++i)
                {
                    (<SwfMovieClip>arr[i]).stop(stopChild);
                }
            }
        }
    }

    public gotoAndStop(frame: Object, stopChild: boolean = true): void
    {
        this.goto(frame);
        this.stop(stopChild);
    }

    public gotoAndPlay(frame: Object): void
    {
        this.goto(frame);
        this.play();
    }

    private goto(frame: any): void
    {
        if ("string" == typeof(frame))
        {
            let labelData: any[] = this.getLabelData(frame);
            if (labelData.length < 3)
            {
                //
                return;
            }
            this.curLabel = labelData[0];
            this.curFrame = this.startFrame = labelData[1];
            this.endFrame = labelData[2];
        }
        else if ("number" == typeof(frame))
        {
            this.curFrame = this.startFrame = frame;
            this.endFrame = this.frames.length - 1;
        }

        this.setCurFrame(this.curFrame);
    }

    private getLabelData(label: string): any[]
    {
        let length: number = this.labels.length;
        let labelData: any[];

        for (let i: number = 0; i < length; ++i)
        {
            labelData = this.labels[i];
            if (labelData[0] == label)
            {
                return labelData;
            }
        }
        return null;
    }

    public isPlaying(): boolean
    {
        return this.isPlay;
    }

    public totalFrames(): number
    {
        return this.frames.length;
    }

    public getCurLabel(): string
    {
        return this.curLabel;
    }

    public allLabels(): any[]
    {
        let length: number = this.labels.length;
        let ret: any[] = [];
        for (let i: number = 0; i < length; ++i)
        {
            ret.push(this.labels[i][0]);
        }
        return ret;
    }

    public hasLabel(label: string): boolean
    {
        let ls: any[] = this.allLabels();
        return !(ls.indexOf(label) == -1);
    }

    public addEventListener(type: string, listener: Function, thisObject: any, useCapture: boolean = false,
        priority: number = 0): void
    {
        super.addEventListener(type, listener, thisObject, useCapture, priority);
        this.hasCompleteListener = this.hasEventListener(egret.Event.COMPLETE);
    }
    
    public removeEventListener(type: string, listener: Function, thisObject: any, useCapture: boolean = false): void
    {
        super.removeEventListener(type, listener, thisObject, useCapture);
        this.hasCompleteListener = this.hasEventListener(egret.Event.COMPLETE);
    }


    public update(): void
    {
        if (!this.isPlay) return;

        if (this.curFrame <= this.endFrame)
        {
            this.setCurFrame(this.curFrame);
            this.curFrame += 1;
            return;
        }

        if (this.hasCompleteListener)
        {
            this.dispatchEventWith(egret.Event.COMPLETE);
        }

        this.curFrame = this.startFrame;
        if (!this.isLoop)
        {
            if (this.ownerSwf)
            {
                this.stop(false);
            }
            return;
        }

        if (this.startFrame == this.endFrame)
        {
            if (this.ownerSwf)
            {
                this.stop(false);
            }
            return;
        }
        this.setCurFrame(this.startFrame);
    }
}

}//end of module starlingswf